package service

import (
	"errors"
	"time"
	"video/consts"
	"video/globals"
	"video/internal/model"
	"video/internal/req"
	"video/internal/resp"
	"video/pkg"
)

type UserServiceImpl interface {
	Login(login *req.ReqLogin) (*resp.LoginResp, error)
}

type UserService struct {
}

func NewUserService() *UserService {
	return &UserService{}
}

func (u UserService) Login(login *req.ReqLogin) (*resp.LoginResp, error) {
	var data model.User
	err := globals.DB.Where("username=?", login.Username).Where("password=?", login.Password).Find(&data).Limit(1).Error
	if err != nil {
		return nil, errors.New("账号或密码不正确")
	}
	if data.Id == 0 {
		return nil, errors.New("账号或密码不正确")
	}

	token, err := pkg.CreateJwtToken(data.Id, time.Hour*2, consts.JWT_TOKEN_SECRET)
	if err != nil {
		return nil, errors.New("token生成失败")
	}

	return &resp.LoginResp{Token: token}, err
}
